package com.myphoto.business.photo.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.myphoto.base.config.properties.IConfig;
import com.myphoto.base.mybatis.photo.dao.PhotoDao;
import com.myphoto.base.mybatis.photo.entity.PhotoEntity;
import com.myphoto.business.photo.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class PhotoServiceImpl implements PhotoService {

    @Resource
    private PhotoDao photoDao;
    @Autowired
    private IConfig config;

    @Override
    public List<PhotoEntity> loadPhotoList() throws Exception {
        LambdaQueryWrapper<PhotoEntity> photoLambdaQueryWrapper = Wrappers.lambdaQuery();
        photoLambdaQueryWrapper.orderByDesc(PhotoEntity::getPicDate);
        return photoDao.selectList(photoLambdaQueryWrapper);
    }

    @Override
    @Transactional
    public void uploadPhoto(String lastModified,MultipartFile file) throws Exception {
        PhotoEntity photoEntity = new PhotoEntity();
        photoEntity.setPicName(file.getOriginalFilename());
        photoEntity.setPicState("1");
        Date xgsj = DateUtil.parse(lastModified,"yyyy-MM-dd HH:mm:ss").toJdkDate();
        photoEntity.setPicDate(xgsj);
        photoDao.insert(photoEntity);

        String photo_path = config.getConfig().getPhoto_path();//得到上传文件主路径
        String filePath = photo_path + file.getOriginalFilename();//得到文件的存储路径
        File newFile = new File(filePath);
        // 检测是否存在目录
        if (!newFile.getParentFile().exists()) {
            //不存在则创建目录
            newFile.getParentFile().mkdirs();
        }
        // 使用 spring 的 transferTo() 方法写入
        file.transferTo(newFile);
    }

    @Override
    public boolean checkFileIsExist(String filename) throws Exception {
        String photo_path = config.getConfig().getPhoto_path();//得到上传文件主路径
        String filePath = photo_path + filename;//得到文件的存储路径
        File newFile = new File(filePath);
        if(newFile.exists()){
            return true;
        }
        return false;
    }

    @Override
    public boolean checkFileInfoIsExist(String filename) throws Exception {
        LambdaQueryWrapper<PhotoEntity> photoLambdaQueryWrapper = Wrappers.lambdaQuery();
        photoLambdaQueryWrapper.eq(PhotoEntity::getPicName,filename);
        photoLambdaQueryWrapper.eq(PhotoEntity::getPicState,"1");
        List list = photoDao.selectList(photoLambdaQueryWrapper);
        if(list!=null && list.size()>0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void clearErrorPicinfo() throws Exception {
        LambdaQueryWrapper<PhotoEntity> photoLambdaQueryWrapper = Wrappers.lambdaQuery();
        List<PhotoEntity> list = photoDao.selectList(photoLambdaQueryWrapper);
        if(list!=null && list.size()>0){
            String photo_path = config.getConfig().getPhoto_path();//得到上传文件主路径
            for(int i=0;i<list.size();i++){
                String picName = list.get(i).getPicName();
                String filePath = photo_path + picName;//得到文件的存储路径
                File newFile = new File(filePath);
                if(newFile.exists()==false){
                    photoDao.deleteById(list.get(i).getId());
                }
            }
        }
    }
}
